﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using SynapseGaming.LightingSystem.Core;
using SynapseGaming.LightingSystem.Components;
using SynapseGaming.LightingSystem.Lights;
using SynapseGaming.LightingSystem.Rendering;
using SynapseGaming.LightingSystem.Serialization;

namespace Game2
{
    public class Animation : BaseComponentAutoSerialization<ISceneEntity>
    {
        float gameSpeed = 1.0f;
        Vector3 position;
        Vector3 addVectorZ = new Vector3(0, 0, -1);
        Vector3 addVectorX = new Vector3(1, 0, 0);

        public override void OnUpdate(GameTime gametime)
        {
            // Cast the Object the component is attached to, to a scene object 
            SceneObject sceneobject = (SceneObject)ParentObject;

            float modelRotation = 0.5f;

            if ((position.Z ==0)||(position.Z <0))
            {
                float moveSpeed = gametime.ElapsedGameTime.Milliseconds / 500.0f * gameSpeed;
                position += addVectorZ * moveSpeed;

                Matrix translationZ = Matrix.CreateTranslation(position);
                sceneobject.World = translationZ * sceneobject.World;

                if (position.Z < -10)
                {
                    float angle = (float)gametime.ElapsedGameTime.TotalSeconds * modelRotation;
                    Matrix rotation = Matrix.CreateRotationY(angle);

                    if (sceneobject.World.M33 > -0.008)
                    {
                        sceneobject.World = rotation * sceneobject.World;
                    }
                    position -= addVectorZ * moveSpeed;
                                   
                }
            }
            //System.Console.WriteLine(position);

        }    
    }
}
